<template>
  <div>
    <el-form ref="form" label-width="100px">
      <el-form-item label="公式编排：">
        <el-input v-model="formula"></el-input>
      </el-form-item>
      <el-form-item label="参数实例：">
        <el-input type="textarea" v-model="parameter"></el-input>
      </el-form-item>
      <el-form-item label="计算结果：">
        <el-input v-model="result" disabled></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="handleClick">提交公式</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import execMathExpress from './mathexpress.js';
export default {
  data() {
    return {
      formula: '(a+b)*c/d',
      parameter: '{"a":"2","b":"3","c":"4","d":"5"}',
      result: '0'
    };
  },
  methods: {
    handleClick() {
      const obj = JSON.parse(this.parameter);
      const res = execMathExpress(this.formula, obj);
      this.result = (res.num / res.den).toFixed(2).toString();
    }
  }
};
</script>
<style lang="scss" scoped></style>
